
package webooksfx.model.parser;

import java.io.InputStream;
import java.net.URLDecoder;
import javafx.data.pull.Event;
import javafx.data.pull.PullParser;
import webooksfx.model.ColeccionLibros;
import webooksfx.model.Libro;
import webooksfx.model.Servicio;

/**
 * @author matias
 */

public class WebooksParser {
    public var input_res: InputStream;
    var type_xml: String = "";

    // Objetos buffer del modelo
    var coleccion: ColeccionLibros = null;
    var libro: Libro = null;
    var servicios: Servicio[];
    var servicio_buffer: Servicio = null;

    var decoder: URLDecoder = URLDecoder {
    }
    var parser: PullParser = PullParser {
        input: bind input_res
        onEvent: parseXML
    }

    function parseXML (e : Event) {
        // OBTENER EL TIPO DE XML
        if ((e.type == PullParser.START_ELEMENT) and ((e.qname.toString()).equals("root"))) {
            var type_xml_name: String = e.getAttributeNames()[0].name.toString();
            if (type_xml_name.equals("value"))
            type_xml = e.getAttributeValue(type_xml_name);
        }
        // VALIDAR SI LA RESPUESTA XML ES V�LIDA
        if ((e.type == PullParser.TEXT) and ((e.qname.toString()).equals("valido"))) {
            if ((e.text.toString()).equals("true")) { // EL XML ES V�LIDO, SETEAR EL PARSER CORRESPONDIENTE
                if (type_xml.equals("search")) {
                    parser.onEvent = parseSearch
                } else if (type_xml.equals("detail")) {
                    parser.onEvent = parseDetail
                } else if (type_xml.equals("services")) {
                    parser.onEvent = parseServices
                }
            }
        }
    }

    function parseSearch (e : Event) {
        if ((e.type == PullParser.TEXT) and ((e.qname.toString()).equals("cantidad"))) {
            this.coleccion = ColeccionLibros {
                cant_resultados: Integer.parseInt(e.text);
            };
        } else if ((e.type == PullParser.START_ELEMENT) and ((e.qname.toString()).equals("libro"))) { //COMIENZA UN LIBRO
            if (this.libro != null) //SI EXISTE UN OBJETO Libro, se agrega a la coleccion.
            this.coleccion.addLibro(this.libro);
            this.libro = Libro {
            };
        } else if ((e.type == PullParser.TEXT) and (e.text.trim().length() > 0)) {
            if ((e.qname.toString()).equals("id_servicio")) {
                this.libro.id = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("isbn")) {
                this.libro.isbn = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("autor")) {
                this.libro.addAutor(decoder.decode(e.text, "UTF8"));
            } else if ((e.qname.toString()).equals("titulo")) {
                this.libro.titulo = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("editorial")) {
                this.libro.editorial = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("miniatura")) {
                this.libro.img_mini = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("url_detalle")) {
                this.libro.url_detalle = decoder.decode(e.text, "UTF8");
            }
        } else if ((e.type == PullParser.END_DOCUMENT) and ((e.qname.toString()).equals("root"))) {
            if (this.libro != null)
            this.coleccion.addLibro(this.libro);
        }
    }
    function parseDetail (e : Event) {
        if ((e.type == PullParser.START_ELEMENT) and ((e.qname.toString()).equals("libro"))) { //COMIENZA UN LIBRO
            this.libro = Libro {
            };
        } else if ((e.type == PullParser.TEXT) and (e.text.trim().length() > 0)) {
            if ((e.qname.toString()).equals("id_servicio")) {
                this.libro.id = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("isbn")) {
                this.libro.isbn = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("autor")) {
                this.libro.addAutor(decoder.decode(e.text, "UTF8"));
            } else if ((e.qname.toString()).equals("titulo")) {
                this.libro.titulo = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("editorial")) {
                this.libro.editorial = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("miniatura")) {
                this.libro.img_mini = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("url_detalle")) {
                this.libro.url_detalle = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("descripcion")) {
                this.libro.descripcion = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("imagen")) {
                this.libro.img_detalle = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("cant_paginas")) {
                this.libro.cant_paginas = decoder.decode(e.text, "UTF8");
			} else if ((e.qname.toString()).equals("fecha")) {
				this.libro.fecha = decoder.decode(e.text, "UTF8");
			}
		}
	}
    function parseServices (e : Event) {
        if ((e.type == PullParser.START_ELEMENT) and ((e.qname.toString()).equals("service"))) { //COMIENZA UN SERVICIO
            if (this.servicio_buffer != null) { //SI EXISTE UN OBJETO Servicio, se agrega a la coleccion.
                insert this.servicio_buffer into this.servicios;
            }
            this.servicio_buffer = Servicio {
            };
        } else if ((e.type == PullParser.TEXT) and (e.text.trim().length() > 0)) {
            if ((e.qname.toString()).equals("id")) {
                this.servicio_buffer.id = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("nombre")) {
                this.servicio_buffer.nombre = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("url_sitio")) {
                this.servicio_buffer.url_sitio = decoder.decode(e.text, "UTF8");
            } else if ((e.qname.toString()).equals("imagen")) {
                this.servicio_buffer.imagen = decoder.decode(e.text, "UTF8");
            }
        } else if ((e.type == PullParser.END_DOCUMENT) and ((e.qname.toString()).equals("root"))) {
            if (this.servicio_buffer != null) {
                insert this.servicio_buffer into this.servicios;
            }
        }
    }

    public function parseResponse() : Void {
        // vaciar buffers
        coleccion = null;
        libro = null;
        delete servicios;
        servicio_buffer = null;

        // setear parser por defecto
        parser.onEvent = parseXML;
        parser.parse();
    }

    public function getColeccionLibros () : ColeccionLibros {
        return this.coleccion;
    }
    public function getLibro () : Libro {
        return this.libro;
    }
    public function getServicios () : Servicio[] {
        return this.servicios;
    }
}
